package t327.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import t327.mapper.StaffManagementMapper;
import t327.pojo.staff.Department;
import t327.pojo.staff.ReimbursementApplication;
import t327.pojo.staff.StaffManagement;
import t327.pojo.staff.StaffPunchCard;
import t327.pojo.staffView.StaffManagementView;
import t327.pojo.staffView.StaffPunchCardView;
import t327.service.iter.IStaffManageService;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StaffManageServiceImpl implements IStaffManageService {
    @Autowired
    private StaffManagementMapper staffManagement;


    @Override
    public int addStaffManagement(t327.pojo.staff.StaffManagement  data) {
        return staffManagement.addStaffManagement(data);
    }

    @Override
    public List<Department> selectStaffDepartment() {

        return staffManagement.selectStaffDepartment();
    }

    @Override
    public List<StaffManagementView> queryAll() {
        List<StaffManagementView> staffManagementViews = staffManagement.queryAll();
        return staffManagementViews;
    }

    @Override
    //查询在职状态
    public int selectStaffStatus(Long id) {
        return staffManagement.selectStaffStatus(id);
    }

    @Override
    public Map<String,String> updateStaffResignationTime(Long id) {

        Map<String,String> map = new HashMap<>();
        StaffManagement Management = new StaffManagement();
        //查询在职状态
        if(selectStaffStatus(id)>0){
            //判断离职是否成功
            if (selectType(id)==1){
                //查询职工的正式工或实习工
                if (staffManagement.updateStaffResignationTime(id,Management.getFutureDate30())>0){
                    map.put("code", "200");
                    map.put("msg", "离职成功");
                    return map;
                }else{
                    map.put("code", "300");
                    map.put("msg", "离职失败");
                    return map;
                }
            }else if(selectType(id)==2){
                if (staffManagement.updateStaffResignationTime(id,Management.getFutureDate3())>0){
                    map.put("code", "200");
                    map.put("msg", "离职成功");
                    return map;
                }else{
                    map.put("code", "300");
                    map.put("msg", "离职失败");
                    return map;
                }
            }else{
                map.put("code", "300");
                map.put("msg", "未知工种");
                return map;
            }

        }else{
            map.put("code", "400");
            map.put("msg", "不可离职或已经离职");
            return map;
        }
    }

    @Override
    public int selectType(Long id) {
        return  staffManagement.selectType(id);
    }

    @Override
    public List<Long> staffResignationTime(LocalDate currentDate) {
        StaffManagement staffManagementProjo = new StaffManagement();
        return staffManagement.SelectStaffDepartment(staffManagementProjo.getCurrentDate());
    }

    @Override
    public Map<String,String> updateStaffResignationTime1() {
        Map<String,String> map = new HashMap<>();
        StaffManagement staffManagementProjo = new StaffManagement();
        System.out.println(staffManagementProjo.getCurrentDate());
        List<Long> list = staffManagement.SelectStaffDepartment(staffManagementProjo.getCurrentDate());
        if(list!=null){
            for (int i = 0; i <list.size(); i++) {
                staffManagement.updateStaffResignationTime1(list.get(i));
            }
            map.put("code", "200");
            map.put("msg", "离职更新啦");
            return map;
        }else{
            map.put("code", "400");
            map.put("msg", "今天没有人离职");;
            return map;
        }
    }

    @Override
    public List<StaffManagement> selectIdStaff(Long id) {
        return staffManagement.selectId(id);
    }

    @Override
    public int updateStaff(StaffManagement staffManagementrProjo) {
        return staffManagement.updateStaff(staffManagementrProjo);
    }

    @Override
    public List<Long> queryAllId() {
        return staffManagement.queryAllId();
    }

    @Override
    public List<String> queryImg(Long id) {
        return staffManagement.queryImg(id);
    }

    @Override
    public int addPunchCard(Long id, Date printTime) {
        StaffPunchCard staffPunchCard = new StaffPunchCard();
        return staffManagement.addPunchCard(id,printTime,staffPunchCard.getHourMinuteSecond());
    }

    @Override
    public boolean addStaffPunchCardTwo() {
        int addStaffPunchCardTwo;
        List<Long> list = staffManagement.queryAllId();
        for (int i = 0; i < list.size(); i++) {
             Long id =list.get(i);
             addStaffPunchCardTwo = staffManagement.addStaffPunchCardTwo(id);
             if (addStaffPunchCardTwo<0){
                 return false;
             }
        }
        return true;
    }

    @Override
    public List<StaffPunchCardView> queryStaffPunchCardView() {
        return staffManagement.queryStaffPunchCardView();
    }

    @Override
    public int updateByThePiece(Long id, Long staffNumberOfCases) {
        return staffManagement.updateByThePiece(id, staffNumberOfCases);
    }

    @Override
    public List<StaffManagement> getAllBoardMaker() {
        return staffManagement.getAllBoardMaker();
    }

    @Override
    public List<StaffManagement> getAllStylist() {
        return staffManagement.getAllStylist();
    }

    @Override
    public boolean addReimbursementApplication(ReimbursementApplication reimbursementApplication) {
        if (staffManagement.addReimbursementApplication(reimbursementApplication)>0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateReissueCard(StaffPunchCardView data) {
        if (staffManagement.updateReissueCard(data)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<StaffPunchCardView> queryStaffPunchCardViewWhere(String name) {
        return staffManagement.queryStaffPunchCardViewWhere(name);
    }

}
